package net.webcursor.lightswitch;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.hardware.Camera;
import android.hardware.Camera.AutoFocusCallback;
import android.hardware.Camera.Parameters;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager.LayoutParams;

public class LightswitchActivity extends Activity {
	
	private static final float BRIGHTNESS_MAX = 1.0f;
	private static final float BRIGHTNESS_MIN = 0.1f;
	
	private static final SoundPool sound = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
	private static Camera camera = null;
	
	private LayoutParams params = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);		
		params = getWindow().getAttributes();
		
		setContentView(new LightswitchView(this));
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();

		// Restore the camera to original state
		try {
			if (camera != null) {
				camera.stopPreview();
				camera.release();
				camera = null;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private class LightswitchView extends View {
		
		private Bitmap background = null;
		private Bitmap lightSwitchOn = null;
		private Bitmap lightSwitchOff = null;
		private int soundOn = -1;
		private int soundOff = -1;
		
		private boolean isTurnedOn = true;
		
		private float density = 1.5f;
		private int width = 0;
		private int height = 0;

		public LightswitchView(Context context) {
			super(context);
			
			initialize();
			invalidate();
		}
		
		private void initialize() {
			width = (int) (getWindowManager().getDefaultDisplay().getWidth());
			height = (int) (getWindowManager().getDefaultDisplay().getHeight()); 
			
			background = BitmapFactory.decodeResource(getResources(), R.drawable.bluewall);
			lightSwitchOff = BitmapFactory.decodeResource(getResources(), R.drawable.ls0);
			lightSwitchOn = BitmapFactory.decodeResource(getResources(), R.drawable.ls1);
			
			soundOff = sound.load(getContext(), R.raw.buttonpress_off, 1);
			soundOn = sound.load(getContext(), R.raw.buttonpress_on, 1);
		}
		
		@Override
		protected void onDraw(Canvas canvas) {
			int buttonWidth = -1;
			int buttonHeight = -1;
			int centerWidth = -1;
			int centerHeight = -1;
			
			canvas.drawBitmap(background, null, new Rect(0, 0, width, height), null);
			
			if (isTurnedOn) {
				buttonWidth = (int) (lightSwitchOn.getWidth() / density);
				buttonHeight = (int) (lightSwitchOn.getHeight() / density);
				centerWidth = width / 2 - buttonWidth / 2;
				centerHeight = height / 2 - buttonHeight / 2; 
				
				// Draw button
				canvas.drawBitmap(lightSwitchOn, null, new Rect(centerWidth, centerHeight, centerWidth + buttonWidth, centerHeight + buttonHeight), null);
				
				// Play sound
				sound.play(soundOn, 1f, 1f, 0, 0, 1);
				
				// Light up screen and backlight
				params.screenBrightness = BRIGHTNESS_MAX;
				params.buttonBrightness = BRIGHTNESS_MAX;
				
				// Lights up backlight LED
				try {
					camera = camera.open();
					Parameters p = camera.getParameters();
					p.setFlashMode(Parameters.FLASH_MODE_ON);
					p.setFocusMode(Parameters.FOCUS_MODE_INFINITY);
					camera.setParameters(p);
				    camera.autoFocus(new AutoFocusCallback() {
		                public void onAutoFocus(boolean success, Camera camera) {
		                }
		            });
					camera.startPreview();
				} catch (Exception ex) {
					ex.printStackTrace();
				}
			} else {
				buttonWidth = (int) (lightSwitchOff.getWidth() / density);
				buttonHeight = (int) (lightSwitchOff.getHeight() / density);
				centerWidth = width / 2 - buttonWidth / 2;
				centerHeight = height / 2 - buttonHeight / 2; 
				
				// Draw button
				canvas.drawBitmap(lightSwitchOff, null, new Rect(centerWidth, centerHeight, centerWidth + buttonWidth, centerHeight + buttonHeight), null);

				// Play sound
				sound.play(soundOff, 1f, 1f, 0, 0, 1);
				
				// Dims screen and backlight
				params.screenBrightness = BRIGHTNESS_MIN;
				params.buttonBrightness = 0;
				
				// Shutdown camera LED
				if (camera != null) {
					camera.stopPreview();
					camera.release();
					camera = null;
				}
			}
			getWindow().setAttributes(params);
			
			super.onDraw(canvas);
		}
		
		@Override
		public boolean onTouchEvent(MotionEvent event) {
			float x = event.getX();
			float y = event.getY();
			
			int buttonWidth = (int) (lightSwitchOff.getWidth() / density);
			int buttonHeight = (int) (lightSwitchOff.getHeight() / density);
			
			int minX = width / 2 - buttonWidth / 2;
			int maxX = width / 2 + buttonWidth / 2;
			
			int minY = height / 2 - buttonHeight / 2;
			int maxY = height / 2 + buttonHeight / 2;
			
			if (x >= minX && x < maxX &&
					y >= minY && y < maxY) {
				isTurnedOn = !isTurnedOn;
				invalidate();
			}
			
			return super.onTouchEvent(event);
		}
		
	}

}
